<template>
  <div>
    <el-card shadown style="margin:20px 0px">
      <CategorySelect :show="scene!=0"></CategorySelect>
    </el-card>

    <!-- SPU列表 -->

    <el-card shadown>
      <div v-show="scene==0">
        <el-button type="primary" style="margin:5px 0" :disabled="!levelThreeId" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="spuList.records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width" align="center">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width" align="center">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width" align="center">
            <template slot-scope="{row}">
              <el-button type="success" icon="el-icon-plus" size="mini" @click="addSku(row)"></el-button>
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="updateSpu(row)"></el-button>
              <el-button type="info" icon="el-icon-info" size="mini" @click="showInfo(row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSpu(row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="getSpuList" :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit" layout=" prev, pager, next, jumper, ->, sizes,total" :total="total" style="margin-top:20px;text-align:center">
        </el-pagination>
      </div>

      <div>
        <SpuForm v-show="scene==1" @changeScene="changeScene" ref="spu"></SpuForm>
        <SkuForm v-show="scene==2" @changeScene="changeScene" ref="sku"></SkuForm>
      </div>

      <el-dialog :title="`${spu.spuName}`" :visible.sync="dialogTableVisible" :before-close="close">
        <el-table :data="skuList" style="width:100%" v-loading="loading">
          <el-table-column prop="skuName" label="名称" width="150"></el-table-column>
          <el-table-column prop="weight" label="价格" width="200"></el-table-column>
          <el-table-column prop="weight" label="重量"></el-table-column>
          <el-table-column label="默认图片">
            <template slot-scope="{row}">
              <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>

  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
import { done } from "nprogress";
export default {
  name: "Spu",
  components: {
    SpuForm,
    SkuForm,
  },
  data() {
    return {
      page: 1,
      limit: 3,
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true,
    };
  },
  computed: {
    ...mapGetters(["levelOneId", "levelTwoId", "levelThreeId"]),
    ...mapState("spu", {
      spuList: (state) => state.spuList || [],
    }),
    total() {
      return this.spuList.total || 0;
    },
  },
  methods: {
    getSpuList(pages = 1) {
      this.page = pages;
      let { page, limit, levelThreeId } = this;
      this.$store.dispatch("spu/getSpuList", { page, limit, levelThreeId });
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == 1) {
        this.getSpuList(this.page);
      }
      if (flag == 0) {
        this.getSpuList();
      }
    },
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    deleteSpu(row) {
      this.$confirm(`是否确认删除${row.spuName}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const result = await this.$API.spu.reqDeleteSpu(row.id);
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getSpuList(
            this.spuList.records.length > 1 ? this.page : this.page - 1
          );
        }
      });
    },
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.levelThreeId);
    },
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(row);
    },
    async showInfo(row) {
      this.dialogTableVisible = true;
      this.spu = row;
      const result = await this.$API.spu.reqSkuList(row.id);
      if (result.code == 200) {
        this.loading = false;
        this.skuList = result.data;
      }
    },
    close(done) {
      this.loading = true;
      this.skuList = [];
      done();
    },
  },
  watch: {
    levelThreeId: {
      handler() {
        this.getSpuList();
      },
    },
  },
};
</script>